﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Model;
using Common;
namespace DAL
{
    public class B
    {
        public static T Entity<T>() where T : class, new()
        {
            string key = typeof(T).Name;
            if (HttpContext.Current != null)
            {
                var bll = HttpContext.Current.Items[key] as T;
                lock (key)
                {
                    if (bll == null)
                    {
                        bll = new T();
                        HttpContext.Current.Items[key] = bll;
                    }
                }
                return bll;
            }
            else
            {
                return new T();
            }
        }
        public static T Instance<T>(T o) where T : class, new()
        {
            if (o == null)
            {
                return new T();
            }
            else
            {
                return o;
            }
        }
        private static string USER_SESSION_KEY = "USER_SESSION_KEY";
        private static string USER_COOKIE_KEY = "USER_COOKIE_KEY";
        private static int USER_COOKIE_EXPIRES = 60; //60分钟
        public static UserInfo MyUser
        {
            get
            {
                var info = HttpContext.Current.Session[USER_SESSION_KEY] as UserInfo;
                
                if (info == null || (info!=null && info.ID==0))
                {
                    string userid = CookieHelper.GetCookie(USER_COOKIE_KEY);
                    if (!string.IsNullOrEmpty(userid))
                    {
                        info = UserDAL.FindOneByID(DataConvert.ToInt(userid));
                        HttpContext.Current.Session[USER_SESSION_KEY] = info;
                        return info;
                    }
                }
                return info;
            }
            set
            {
                if (value != null)
                {
                    HttpContext.Current.Session[USER_SESSION_KEY] = value;
                    Common.CookieHelper.WriteCookie(USER_COOKIE_KEY, value.ID.ToString(), USER_COOKIE_EXPIRES);
                }
                else
                {
                    HttpContext.Current.Session[USER_SESSION_KEY] = null;
                    Common.CookieHelper.WriteCookie(USER_COOKIE_KEY, string.Empty, -USER_COOKIE_EXPIRES);
                }
            }
        }
       


        private static string ADMIN_SESSION_KEY = "JOB_CRM_ADMIN";
        private static string ADMIN_COOKIE_KEY = "JOB_CRM_ADMIN";
        private static int USER_COOKIE_Expires = 60; //60分钟
        public static AdminInfo MyAdmin
        {
            get
            {
                var info = HttpContext.Current.Session[ADMIN_SESSION_KEY] as AdminInfo;
                if (info == null)
                {
                    string userid = CookieHelper.GetCookie(ADMIN_COOKIE_KEY);
                    if (!string.IsNullOrEmpty(userid))
                    {
                        info = AdminDAL.FindOneByID(DataConvert.ToInt(userid));
                        HttpContext.Current.Session[ADMIN_SESSION_KEY] = info;
                        return info;
                    }
                }
                return info;
            }
            set
            {
                if (value != null)
                {
                    HttpContext.Current.Session[ADMIN_SESSION_KEY] = value;
                    CookieHelper.WriteCookie(ADMIN_COOKIE_KEY, value.ID.ToString(), USER_COOKIE_Expires);
                }
                else
                {
                    HttpContext.Current.Session[ADMIN_SESSION_KEY] = null;
                    CookieHelper.WriteCookie(ADMIN_COOKIE_KEY, string.Empty, -USER_COOKIE_Expires);
                }
            }
        }
    }
}
